package com.add.system;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CardSetTable {
  private static final Log _log = LogFactory.getLog(CardSetTable.class);
  
  private static CardSetTable _instance;
  
  private static final Map<Integer, CardPolySet> _cardIndex = new HashMap<>();
  
  public static CardSetTable get() {
    if (_instance == null)
      _instance = new CardSetTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int t = 0;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `w_变身卡片能力组合套卡`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int id = rs.getInt("流水号");
        String msg1 = rs.getString("组合套卡名称");
        String needisd = rs.getString("需求的变身卡编号").replaceAll(" ", "");
        String[] needi_tmp = needisd.split(",");
        int[] needids = new int[needi_tmp.length];
        int i = 0;
        while (i < needi_tmp.length) {
          needids[i] = Integer.parseInt(needi_tmp[i]);
          i++;
        } 
        String needquest = rs.getString("需求的玩家任务编号").replaceAll(" ", "");
        String[] needq_tmp = needquest.split(",");
        int[] needquests = new int[needq_tmp.length];
        int k = 0;
        while (k < needq_tmp.length) {
          needquests[k] = Integer.parseInt(needq_tmp[k]);
          k++;
        } 
        String needname = rs.getString("需求的变身卡名称").replaceAll(" ", "");
        String[] needn_tmp = needname.split(",");
        String[] neednames = new String[needn_tmp.length];
        int j = 0;
        while (j < needn_tmp.length) {
          neednames[j] = needn_tmp[j];
          j++;
        } 
        int questid = rs.getInt("任务纪录编号");
        int str = rs.getInt("力量");
        int dex = rs.getInt("敏捷");
        int con = rs.getInt("体质");
        int Int = rs.getInt("智力");
        int wis = rs.getInt("精神");
        int cha = rs.getInt("魅力");
        int ac = rs.getInt("防御");
        int hp = rs.getInt("血量");
        int mp = rs.getInt("魔量");
        int hpr = rs.getInt("回血量");
        int mpr = rs.getInt("回魔量");
        int dmg = rs.getInt("近距离伤害");
        int bowdmg = rs.getInt("远距离伤害");
        int hit = rs.getInt("近距离命中");
        int bowhit = rs.getInt("远战攻击命中");
        int dmgr = rs.getInt("物理减免伤害");
        int mdmgr = rs.getInt("魔法减免伤害");
        int sp = rs.getInt("魔攻");
        int mhit = rs.getInt("魔法命中");
        int mr = rs.getInt("魔法防御");
        int fire = rs.getInt("火属性防御");
        int water = rs.getInt("水属性防御");
        int wind = rs.getInt("风属性防御");
        int earth = rs.getInt("地属性防御");
        CardPolySet card = new CardPolySet(id, msg1, needids, needquests, neednames, questid, str, dex, 
            con, Int, wis, cha, ac, hp, mp, hpr, mpr, dmg, bowdmg, hit, bowhit, dmgr, mdmgr, sp, mhit, mr, 
            fire, water, wind, earth);
        _cardIndex.put(Integer.valueOf(id), card);
        t++;
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("读取->w_变身卡片能力组合套卡系统: " + t + "(" + timer.get() + "ms)");
  }
  
  public int CardCardSize() {
    return _cardIndex.size();
  }
  
  public CardPolySet getCard(int id) {
    return _cardIndex.get(Integer.valueOf(id));
  }
}
